package org.lvhengchang.schoolmanager.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lvhengchang.schoolmanager.bean.Student;
import org.lvhengchang.schoolmanager.service.StudentService;
import org.lvhengchang.schoolmanager.service.impl.StudentServiceImpl;

/**
 * 学生修改操作处理类
 * @author LvHengchang
 *
 */
@WebServlet("/student/update")
public class StudentUpdateServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private StudentService studentService = new StudentServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String student_id = req.getParameter("student_id");

		try {
			Student student = studentService.selectById(student_id);
			req.setAttribute("student", student);
			req.getRequestDispatcher("/studentUpdate.jsp").forward(req, resp);
		} catch (Exception e) {
			throw new ServletException(e);
		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");

		String class_id =  req.getParameter("class_id");
		String student_name = (String) req.getParameter("student_name");
		String student_birthday = (String) req.getParameter("student_birthday");
		String sex = (String) req.getParameter("sex");
		String remarks = (String) req.getParameter("remarks");
		String student_id = req.getParameter("student_id");

		Student student = new Student();
		student.setClass_id(class_id);
		student.setStudent_name(student_name);
		student.setStudent_birthday(student_birthday);
		student.setSex(sex);
		student.setRemarks(remarks);
		student.setStudent_id(student_id);

		try {
			studentService.updateStudent(student);
			req.setAttribute("class_id", class_id);
			req.setAttribute("student_id", student_id);
			req.setAttribute("student_name", student_name);
			req.setAttribute("student_birthday", student_birthday);
			req.setAttribute("sex", sex);
			req.setAttribute("remarks", remarks);
			req.getRequestDispatcher("/student/student_update_success.jsp").forward(req, resp);
		} catch (Exception e) {
			throw new ServletException(e);
		}

	}

}
